# Copyright (c) 2023, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe import _
from frappe.model.document import Document

class SRMSupplierMoreNature(Document):
	@frappe.whitelist()
	def validate_company_nature(self):
		# 验证当前供应商及公司类别是否已经存在
		if frappe.db.exists("SRM Supplier", {"name": self.supplier_number, "company_nature": self.company_nature}) or frappe.db.exists("SRM Supplier More Nature", {"supplier_number": self.supplier_number, "company_nature": self.company_nature}):
			return 'exists'
			# frappe.throw(title=_('Error'), msg=_(f'供应商:{self.supplier_number} 并且公司类别:{self.company_nature} 已经存在!不允许重复!'))
		else:return 'not exists'
		
	def after_insert(self):
		self.append_basic_date_pdf()
		self.append_certificate()
		self.append_certificate_3c_pdf()
		self.append_investigate()
		self.append_contract_agreement()

	@frappe.whitelist()
	def append_basic_date_pdf(self):
		# 先清空子表
		self.basic_date_pdf = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Basic Date PDF', 'company_nature': self.company_nature},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('basic_date_pdf', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_certificate(self):
		# 先清空子表
		self.certificate = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Certificate PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('certificate', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_certificate_3c_pdf(self):
		# 先清空子表
		self.certificate_3c_pdf = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Certificate 3C PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('certificate_3c_pdf', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_investigate(self):
		# 先清空子表
		self.investigate = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Investigate PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('investigate', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_contract_agreement(self):
		# 先清空子表
		self.contract_agreement = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Contract And Agreement'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('contract_agreement', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()
